package com.example.cloudstreamrocketmqprovider8801.conf;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

/**
 * @ClassName: RocketMqHealthCheck
 * @Description:
 * @Date 2024/04/07 16:09
 * @Author songshaoqiu
 **/
@Component
@RequiredArgsConstructor
@Slf4j
public class RocketMqHealthCheck extends AbstractHealthIndicator {
    private final RocketMQTemplate rocketMQTemplate;


    protected void doHealthCheck(Health.Builder builder)  {
        try {
            log.info("健康信息----", "aa");
            Message<String> message = MessageBuilder.withPayload("test")
                    .build();
            SendResult sendResult = rocketMQTemplate.syncSend("test-topic", message);
            log.info("发送结果{}",sendResult);
            if (sendResult.getSendStatus().equals(SendStatus.SEND_OK)){
                builder.up();
            }else{
                builder.unknown();
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error("发送失败", e.getMessage());
            builder.down();

        }
    }

}
